
***************************************************************;
* Purpose: generate a comprehensive plan characterisc dataset *;
*          combine plan information from hedis datasets       *;
*          and additional information from RICK               *;
*          and accredition info from Jeff                     *;
* Date:    3-8-2012         Last Update:  4-4-2013            *;
***************************************************************;

options ls=120 nofmterr;

libname plan '/data/Medicare_P01_2009/data/HEDIS/plandata';
libname new  '/data/Medicare_P01_2009/data/HEDIS/sasdata';


  *** Dataset 1: Accredition Information ***;

    %macro accred(yr,yr2,outdat);

       data c00; set plan.plans_accred;
             if hedisyr = &yr ;
             accredited_&yr2 = accredited ;

       data &outdat; set c00;
             keep accredited_&yr2 org_id;
             proc sort; by org_id;

    %mend;

    %accred(2004,03,c04);
    %accred(2005,04,c05);
    %accred(2006,05,c06);
    %accred(2007,06,c07);
    %accred(2008,07,c08);
    %accred(2009,08,c09);
  
  data c0; merge c04 c05 c06 c07 c08 c09; by org_id; 

       in_accred=1;
       proc sort; by org_id;


 *** Dataset 2: HMO Plans >=500 In Analysis ***;


 data s0; set new.selectplans;

      keep org_id  
           in_2003 totn_03
           in_2004 totn_04
           in_2005 totn_05
           in_2006 totn_06
           in_2007 totn_07
           in_2008 totn_08
           in_2009 totn_09 ;

      proc means n mean min median max maxdec=0 fw=6;
           var totn_03 totn_04 totn_05 totn_06 totn_07 totn_08 totn_09;
           title 'Plans in SELECT_PLANS dataset';

      proc sort; by org_id;

 
  *** Dataset 3: All HMO/PPO plans in HEDIS datasets after basic exclusions ***;

      %macro plans(indat,yr,outdat);

         data &outdat(drop=tot_n); set &indat; 
           
              if tot_n >=500;    /** keep only plans with >=500 respondents **/

              length plansize_&yr $ 10;
              if tot_n>=25000 then plansize_&yr ='Large>=25k';
              if tot_n<25000  then plansize_&yr ='Small<25k';

              hed_&yr    = 1;
              totn_&yr   = tot_n;
                              
              keep org_id org_name tot_n plantypec 
                   hed_&yr totn_&yr plansize_&yr;
              proc freq; tables plantypec;
         
              proc sort; by org_id;
       
      %mend;
      %plans(plan.cntr2003all4,2003,y03);
      %plans(plan.cntr2004all4,2004,y04);
      %plans(plan.cntr2005all4,2005,y05);
      %plans(plan.cntr2006all4,2006,y06);
      %plans(plan.cntr2007all4,2007,y07);
      %plans(plan.cntr2008all4,2008,y08);
      %plans(plan.cntr2009all4,2009,y09);


   data h0; merge y03 y04 y05 y06 y07 y08 y09; by org_id;

        in_hedis =1;

        if hed_2008=1 and org_id='H7949' then delete;  
            /** this plan no name with 1000+ cases, decided to remove in final runs of HMOFFS analysis **/
 
 
   *** Dataset 3: Additional Plan Char Info obtained by RICK ***;
   

     data p_2003;  set plan.planinfo2003; in_2003=1; proc sort; by org_id;  
     data p_2004;  set plan.planinfo2004; in_2004=1; proc sort; by org_id;  
     data p_2005;  set plan.planinfo2005; in_2005=1; proc sort; by org_id;  

        data p_2006_0;  set plan.planinfo2006; in_2006=1; rename taxstatus06=tax06; 
        data p_2007_0;  set plan.planinfo2007; in_2007=1; rename taxstatus07=tax07;
        data p_2008_0;  set plan.planinfo2008; in_2008=1; rename taxstatus08=tax08;

        data p_2009_0;  set plan.planinfo2009; in_2009=1; 
                            rename tax_status_code=tax09;
                            rename national_affiliation = national_affiliation09;
 
     data p_2006(drop=tax06); set p_2006_0;  length taxstatus06 $ 3; 
               if tax06=1 then taxstatus06='PRO';
               if tax06=2 then taxstatus07='NON'; proc sort; by org_id;

     data p_2007(drop=tax07); set p_2007_0;   length taxstatus07 $ 3;
               if tax07=1 then taxstatus07='PRO';
               if tax07=2 then taxstatus07='NON'; proc sort; by org_id;

     data p_2008(drop=tax08); set p_2008_0;   length taxstatus08 $ 3;
               if tax08=1 then taxstatus08='PRO';
               if tax08=2 then taxstatus08='NON'; proc sort; by org_id;

     data p_2009(drop=tax09); set p_2009_0;  length taxstatus09 $ 3;
               if tax09=1 then taxstatus09='PRO';
               if tax09=2 then taxstatus09='NON'; proc sort; by org_id;


     data rick0; merge p_2003 p_2004 p_2005 p_2006 p_2007 p_2008 p_2009; by org_id;

          in_rick =1;
         
          keep in_rick org_id
               national_affiliation03 
               national_affiliation08
               national_affiliation09
               taxstatus03 startdate03 model03
               taxstatus04 startdate04 model04
               taxstatus05 startdate05 
               taxstatus06 startdate06
               taxstatus07 startdate07
               taxstatus08 startdate08 
               taxstatus09 ;         
 
 
      *-- Separate Northern and Sourthern KS to merge with A01 and A02 --*;

      data ks01; set rick0; if org_id='H0524';  org_id = 'HS524' ;
      data ks02; set rick0; if org_id='H0524';  org_id = 'HN524';
      data noks; set rick0; if org_id='H0524' then delete;

 
  data r0; set noks ks01 ks02; 

      proc sort; by org_id;



 ********************************************************************;
 * Combine A01 A02 A03 To produce a comprehensive HEDIS plan data   *;
 ********************************************************************;
 
 data one; merge c0 s0 h0(in=ok) r0; by org_id;

      if ok;

      ** Recode Missing Data by Year **;

        *-- Fill in for the 11 missing cases in Rick File for HEDIS 2003 data --*;
           
           length affiliation03 $ 10;
           
           if hed_2003=1 and taxstatus03=' ' then taxstatus03=taxstatus04;
           if hed_2003=1 and org_id='H1961' then taxstatus03='PRO';  /** used taxstatus06 data **/

           if hed_2003=1 and model03=' '   then model03=model04;
           if hed_2003=1 and startdate03=. then startdate03=startdate04;
           if hed_2003=1 then yrs_start03 = 2003-year(startdate03);

           if hed_2003=1 and national_affiliation03=1 then affiliation03='National';
           if hed_2003=1 and national_affiliation03=0 then affiliation03='Local';
 
           ** Affiliation Information Additions **;
           if hed_2003=1 and org_id='H2204' then affiliation03='Local';
           if hed_2003=1 and org_id='H2206' then affiliation03='Local';

        *-- Fill in for the 1 missing case in Rick File for HEDIS 2004 data --*;

           if hed_2004=1 and org_id='H1961' then taxstatus04='PRO';  /** used taxstatus06 data **/
           if hed_2004=1 then yrs_start04 = 2004-year(startdate04);

        *-- Fill in for missing case in Rick File for HEDIS 2005 data --*;

           if hed_2005=1 and org_id='H1961' then taxstatus05='PRO';  /** used taxstatus06 data **/
           if hed_2005=1 then yrs_start05 = 2005-year(startdate05);

        *-- Fill in for missing cases in Rick File for HEDIS 2006 data --*;

           if hed_2006=1 and taxstatus06=' ' then taxstatus06=taxstatus07;
           if hed_2006=1 and startdate06=. then startdate06=startdate07;
           if hed_2006=1 then yrs_start06 = 2006-year(startdate06);

         *-- for HEDIS 2007 and HEDIS 2008 --*;

           if hed_2007=1 then yrs_start07 = 2007-year(startdate07);
           if hed_2008=1 then yrs_start08 = 2008-year(startdate08);


         *** Creating Affilation 2008 Variable ***;

            length affiliation08 $ 10;
            if national_affiliation08=1 then affiliation08='National';
            if national_affiliation08=0 then affiliation08='Local';

            length affiliation09 $ 10;
            if national_affiliation09=1 then affiliation09='National';
            if national_affiliation09=0 then affiliation09='Local';


 
            length affiliation04 
                   affiliation05 
                   affiliation06 
                   affiliation07 $ 10;

            if in_2003=1 and affiliation03=' ' then affiliation03=affiliation08;

            if in_2004=1 then affiliation04=affiliation03;
            if in_2004=1 and affiliation04=' ' then affiliation04=affiliation08;

            if in_2005=1 then affiliation05=affiliation03;
            if in_2005=1 and affiliation05=' ' then affiliation05=affiliation08;

            if in_2006=1 then affiliation06=affiliation08;
            if in_2006=1 and affiliation06=' ' then affiliation06=affiliation03;

            if in_2007=1 then affiliation07=affiliation08;
            if in_2007=1 and affiliation07=' ' then affiliation07=affiliation03;

            if in_2008=1 and affiliation08=' ' then affiliation08=affiliation03;

            ** Add missing Affiliation Info data from Rick **;

            if org_id='H0538' then do; affiliation04='Local';    affiliation05='Local';    end;
            if org_id='H3112' then do; affiliation06='National'; affiliation07='National'; end;
            if org_id='H3351' then do; affiliation05='Local'; end;
            if org_id='H5149' then do; affiliation03='Local'; end;
            if org_id='H5440' then do; affiliation07='National'; end;
            if org_id='H5586' then do; affiliation07='Local'; end;


            ** Clean Accredation Variable set missing to 0 if In that year **;

            if hed_2003=1 and accredited_03=. then accredited_03=0;  if hed_2003=. then accredited_03=.;
            if hed_2004=1 and accredited_04=. then accredited_04=0;  if hed_2004=. then accredited_04=.; 
            if hed_2005=1 and accredited_05=. then accredited_05=0;  if hed_2005=. then accredited_05=.;
            if hed_2006=1 and accredited_06=. then accredited_06=0;  if hed_2006=. then accredited_06=.;
            if hed_2007=1 and accredited_07=. then accredited_07=0;  if hed_2007=. then accredited_07=.;
            if hed_2008=1 and accredited_08=. then accredited_08=0;  if hed_2008=. then accredited_08=.;

          
            ** Create New/Old Plan Variable **;

           length newplan $ 12 ; label newplan ='Year Plan entered';

                if hed_2003=1 and hed_2004=1 and hed_2005=1 and hed_2006=1 and hed_2007=1 and hed_2008=1 and hed_2009=1 then newplan='In 7 years';
           else if hed_2003=1 then newplan='New In 2003';
           else if hed_2003=. and hed_2004=1 then newplan='New In 2004';
           else if hed_2003=. and hed_2004=. and hed_2005=1 then newplan ='New In 2005';
           else if hed_2003=. and hed_2004=. and hed_2005=. and hed_2006=1 then newplan='New In 2006';
           else if hed_2003=. and hed_2004=. and hed_2005=. and hed_2006=. and hed_2007=1 then newplan='New In 2007';
           else if hed_2003=. and hed_2004=. and hed_2005=. and hed_2006=. and hed_2007=. and hed_2008=1 then newplan='New In 2008';
           else if hed_2003=. and hed_2004=. and hed_2005=. and hed_2006=. and hed_2007=. and hed_2008=. and hed_2009=1 then newplan='New In 2009';

           
           *** Test *** ;

           proc freq;
                tables taxstatus09*(taxstatus03 taxstatus04 taxstatus05 taxstatus06 taxstatus07 taxstatus08) 
                       affiliation09*(affiliation03 affiliation08)/missing nopercent;


  ** For Jeff **;

 data new.hmoplans; set one;

      if plantypec='HMO';
 run;

 ** For Store in PLANDATA directory **;

 data plan.hedisplans_all; set one;

      ** Include both PPO and HMO **;
  run;

 data plan.hedisplans_hmo; set one;

      if plantypec='HMO';
 run;
endsas;

